import cv2
import numpy as np


#全局变量
#设置人所在的位置和人的大小
people_height = 1200     #人像缩放高度
people_width = 800      #人像缩放宽度
people_loc_x = 900      #人像定位（从左上角开始）
people_loc_y = 0     #代码来源于http://me.zhengqiao.wang/
#打开人像图片
img = cv2.imread('C:/Users/wren1/Desktop/1.jpg')
#打开背景图片
img_background = cv2.imread("C:/Users/wren1/Desktop/TIM20190518132817.jpg")

##初始化
result = np.zeros((int(img_background.shape[0]),int(img_background.shape[1]),3),np.uint8)
result[:,:,0],result[:,:,1],result[:,:,2] = 255,0,0  #代码来源于http://me.zhengqiao.wang/

#执行代码
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
blue=cv2.inRange(hsv,np.array([78,43,46]),np.array([155,255,255]))
contours, hierarchy = cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #代码来源于http://me.zhengqiao.wang/
for i in range(len(contours)):
    area = cv2.contourArea(contours[i])#代码来源于http://me.zhengqiao.wang/
    if area > 200:
        #填充为蓝色
        cv2.drawContours(img,[contours[i]],-1,(255,0,0),-1)
        pass
people = cv2.resize(img,(people_width,people_height))
result[people_loc_y:people_loc_y+people_height,people_loc_x:people_loc_x+people_width] = people #代码来源于http://me.zhengqiao.wang/
background = cv2.inRange(result,np.array([255,0,0]),np.array([255,0,0]))
tmp_back = cv2.bitwise_and(img_background,img_background,mask=background) #代码来源于http://me.zhengqiao.wang/
tmp_people = cv2.bitwise_and(result,result,mask=cv2.bitwise_not(background))
result = cv2.add(tmp_back,tmp_people)

#显示结果
#为了显示方便缩放一下结果
img_result = cv2.resize(result,(600,800)) #代码来源于http://me.zhengqiao.wang/
cv2.imshow("hole",img_result)
cv2.waitKey(0)